package com.jutixueyuan.service.impl;

import com.jutixueyuan.pojo.Banner;
import com.jutixueyuan.pojo.LivegoodsResult;
import com.jutixueyuan.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Value("${livegoods.fastdfs.nginx}")
    private String livegoodsFastDfsNginx;

    @Override
    public LivegoodsResult selectBanners() {
        try{
            //查询所有数据
            List<Banner> banners = mongoTemplate.findAll(Banner.class);

            //创建集合，用于封装banner的路径
            List<String> paths = new ArrayList<>();
            for (Banner banner : banners) {
                //拼接远程服务器地址
                paths.add(this.livegoodsFastDfsNginx+banner.getPath());
            }
            //组装LivegoodsResult对象
            LivegoodsResult livegoodsResult = LivegoodsResult.ok();
            livegoodsResult.setResults(paths);
            return livegoodsResult;
        }catch (Exception e){
            e.printStackTrace();
            return LivegoodsResult.error("请求失败");
        }

    }
}
